package com.yunzheng.modoules.enrollAndLogin.login.dao;

import com.yunzheng.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl implements UserDao{

    private static Connection connection;

    @Override
    public String queryPwd(String phone_num) throws Exception {
        String password = null;
        Connection connection = DBUtils.getCon();
        String sql = "select * from user_info where phone_num = ?;";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,phone_num);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            password = resultSet.getString("password");
        }
        DBUtils.release(resultSet,preparedStatement,connection);
        return password;
    }

    @Override
    public boolean insert(String phone_num, String password,String username,String term,String direction,String gender) throws Exception {
        connection = DBUtils.getCon();
        String sql = "insert into user_info (phone_num,password,username,term,direction,gender) values (?,?,?,?,?,?);";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,phone_num);
        preparedStatement.setString(2,password);
        preparedStatement.setString(3,username);
        preparedStatement.setString(4,term);
        preparedStatement.setString(5,direction);
        preparedStatement.setString(6,gender);
        int count = preparedStatement.executeUpdate();
        if (count!=0){
            return true;
        }
        DBUtils.release(null,preparedStatement,connection);
        return false;
    }

    @Override
    public int queryId(String phone_num) throws Exception {
        int id = 0;
        connection = DBUtils.getCon();
        String sql = "select id from user_info where phone_num = ?;";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,phone_num);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            id = resultSet.getInt("id");
        }
        DBUtils.release(resultSet,preparedStatement,connection);
        return id;
    }


}

